package com.pai4j.common.enums;

import lombok.Getter;

@Getter
public enum RolePermissionCodeEnum {

    USER(1, new PermissionCodeEnum[]{PermissionCodeEnum.ADD, PermissionCodeEnum.UPDATE, PermissionCodeEnum.READ, PermissionCodeEnum.DELETE,PermissionCodeEnum.UPDATE, PermissionCodeEnum.READ}),
    ADMIN(2, new PermissionCodeEnum[]{PermissionCodeEnum.ADD, PermissionCodeEnum.DELETE, PermissionCodeEnum.UPDATE, PermissionCodeEnum.READ, PermissionCodeEnum.DISABLE_USER, PermissionCodeEnum.ASSIGN_ROLE, PermissionCodeEnum.PUBLISH_ARTICLE, PermissionCodeEnum.DELETE_COMMENT, PermissionCodeEnum.AUDIT_CONTENT, PermissionCodeEnum.CREATE_ORDER, PermissionCodeEnum.CANCEL_ORDER, PermissionCodeEnum.REFUND_ORDER, PermissionCodeEnum.EXPORT_ORDER_DATA, PermissionCodeEnum.IMPORT_DATA, PermissionCodeEnum.EXPORT_DATA, PermissionCodeEnum.BATCH_PROCESS}),
    GUEST(3, new PermissionCodeEnum[]{PermissionCodeEnum.READ}),
    ROBOT(6, new PermissionCodeEnum[]{PermissionCodeEnum.READ, PermissionCodeEnum.CREATE_ORDER});

    private Integer roleCode;
    private PermissionCodeEnum[] permissions;

    RolePermissionCodeEnum(Integer roleCode, PermissionCodeEnum[] permissions) {
        this.roleCode = roleCode;
        this.permissions = permissions;
    }
   
    
    /**
     * 判断当前角色是否有某个权限
     *
     * @param roleCode 角色编码
     * @param permission 权限编码
     * @return true/false
     */
    public static boolean hasPermission(Integer roleCode, PermissionCodeEnum permission) {
        if (roleCode == null || permission == null) {
            return false;
        }
        for (RolePermissionCodeEnum rolePermission : RolePermissionCodeEnum.values()) {
            if (rolePermission.getRoleCode().equals(roleCode)) {
                for (PermissionCodeEnum perm : rolePermission.getPermissions()) {
                    if (perm.equals(permission)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
}